package leetcode.list;

import utils.node.ListNode;

/**
 * @Description: https://leetcode.cn/problems/linked-list-cycle-ii/description/
 * @Author Ammar
 * @Create 2023/7/30 17:55
 */
public class _142_环形链表II {
    public ListNode detectCycle(ListNode head) {
        ListNode fast = head, slow = head;
        while (true) {
            if (fast == null || fast.next == null) return null;
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) break;
        }
        fast = head;
        while (slow != fast) {
            slow = slow.next;
            fast = fast.next;
        }
        return fast;
    }
}
